package com.fare.taluo.em;

import java.util.Calendar;
import java.util.Date;

/**
 * 时间段枚举
 * <p>将时间拆分成，上午，中午，下午，晚上，各自有对应的时间段
 * @author xiangsf 2013-05-24
 *
 */
public enum TimePeriod {

	ALL("ALL","所有"), 
	AM("AM","上午"), 
	MD("MD","中午"), 
	PM("PM","下午"), 
	ND("ND","晚上"), 
	OTHER("OTHER","自定义");
	
	private String code;
	private String name;
	private String _hhmm;
	private String hhmm_;
	private String _hhmmss;
	private String hhmmss_;
	private TimePeriod(String code, String name){
		this.code = code;
		this.name = name;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * 比较指定时间，是否在枚举的时间段内
	 * @param date
	 * @return
	 */
	public boolean hasBetweenTime(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		Calendar startCal = Calendar.getInstance();
		startCal.setTime(date);
		startCal.set(Calendar.MINUTE, 0);
		startCal.set(Calendar.SECOND, 0);
		Calendar endCal = Calendar.getInstance();
		endCal.setTime(date);
		endCal.set(Calendar.MINUTE, 0);
		endCal.set(Calendar.SECOND, 0);
		if("ALL".equalsIgnoreCase(this.getCode())){
			return true;
		}else if("AM".equalsIgnoreCase(this.getCode())){//时间从6:00-12:00
			startCal.set(Calendar.HOUR_OF_DAY, 6);
			endCal.set(Calendar.HOUR_OF_DAY, 12);
			return date.equals(endCal.getTime())
					|| date.equals(startCal.getTime())
					|| (date.before(endCal.getTime()) && date.after(startCal.getTime()));
		}else if("MD".equalsIgnoreCase(this.getCode())){//时间从12:00-14:00
			startCal.set(Calendar.HOUR_OF_DAY, 12);
			endCal.set(Calendar.HOUR_OF_DAY, 14);
			return date.equals(endCal.getTime())
					|| date.equals(startCal.getTime())
					|| (date.before(endCal.getTime()) && date.after(startCal.getTime()));
		}else if("PM".equalsIgnoreCase(this.getCode())){ //时间从14:00-18:00
			startCal.set(Calendar.HOUR_OF_DAY, 14);
			endCal.set(Calendar.HOUR_OF_DAY, 16);
			return date.equals(endCal.getTime())
					|| date.equals(startCal.getTime())
					|| (date.before(endCal.getTime()) && date.after(startCal.getTime()));
		}else if("ND".equalsIgnoreCase(this.getCode())){//时间从00:00-06:00 , 18:00-24:00
			startCal.set(Calendar.HOUR_OF_DAY, 0);
			endCal.set(Calendar.HOUR_OF_DAY, 6);
			boolean first = date.equals(endCal.getTime())
					|| date.equals(startCal.getTime())
					|| (date.before(endCal.getTime()) && date.after(startCal.getTime()));
			if(!first){
				startCal.set(Calendar.HOUR_OF_DAY, 18);
				endCal.set(Calendar.HOUR_OF_DAY, 24);
				return date.equals(endCal.getTime())
						|| date.equals(startCal.getTime())
						|| (date.before(endCal.getTime()) && date.after(startCal.getTime()));
			}
		}
		return false;
	}
	/**
	 * 开始时间，时分秒，hhmm格式
	 * @return
	 */
	public String get_hhmm() {
		if("ALL".equalsIgnoreCase(this.getCode())){
			return "0000";
		}else if("AM".equalsIgnoreCase(this.getCode())){
			return "0600";
		}else if("MD".equalsIgnoreCase(this.getCode())){
			return "1200";
		}else if("PM".equalsIgnoreCase(this.getCode())){
			return "1400";
		}else if("ND".equalsIgnoreCase(this.getCode())){
			return "1800";
		}
		return _hhmm;
	}
	/**
	 * 结束时间，时分，hhmm格式
	 * @return
	 */
	public String getHhmm_() {
		if("ALL".equalsIgnoreCase(this.getCode())){
			return "2400";
		}else if("AM".equalsIgnoreCase(this.getCode())){
			return "1200";
		}else if("MD".equalsIgnoreCase(this.getCode())){
			return "1400";
		}else if("PM".equalsIgnoreCase(this.getCode())){
			return "1800";
		}else if("ND".equalsIgnoreCase(this.getCode())){
			return "0600";
		}
		return hhmm_;
	}
	/**
	 * 开始时间，时分秒，hhmmss格式
	 * @return
	 */
	public String get_hhmmss() {
		if("ALL".equalsIgnoreCase(this.getCode())){
			return "000000";
		}else if("AM".equalsIgnoreCase(this.getCode())){
			return "060000";
		}else if("MD".equalsIgnoreCase(this.getCode())){
			return "120000";
		}else if("PM".equalsIgnoreCase(this.getCode())){
			return "140000";
		}else if("ND".equalsIgnoreCase(this.getCode())){
			return "180000";
		}
		return _hhmmss;
	}
	/**
	 * 结束时间，时分秒，hhmmss格式
	 * @return
	 */
	public String getHhmmss_() {
		if("ALL".equalsIgnoreCase(this.getCode())){
			return "240000";
		}else if("AM".equalsIgnoreCase(this.getCode())){
			return "120000";
		}else if("MD".equalsIgnoreCase(this.getCode())){
			return "140000";
		}else if("PM".equalsIgnoreCase(this.getCode())){
			return "180000";
		}else if("ND".equalsIgnoreCase(this.getCode())){
			return "060000";
		}
		return hhmmss_;
	}
	
	public static TimePeriod fromCode(String code){
		if("ALL".equalsIgnoreCase(code)){
			return TimePeriod.ALL;
		}else if("AM".equalsIgnoreCase(code)){
			return TimePeriod.AM;
		}else if("MD".equalsIgnoreCase(code)){
			return TimePeriod.MD;
		}else if("PM".equalsIgnoreCase(code)){
			return TimePeriod.PM;
		}else if("ND".equalsIgnoreCase(code)){
			return TimePeriod.ND;
		}else if("OTHER".equalsIgnoreCase(code)){
			return TimePeriod.OTHER;
		}
		return TimePeriod.ALL;
	}
}
